package com.qying.myguava.io;

import com.google.common.io.ByteSource;

import javax.annotation.CheckForNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @Author 秦扬
 * @Date 2021/9/16 10:35
 */
final class MultiInputStream extends InputStream {

    private Iterator<? extends ByteSource> it;

    @CheckForNull private InputStream in;

    public MultiInputStream(Iterator<? extends ByteSource> it) throws IOException{
        this.it = checkNotNull(it);
        advance();
    }

    public void close() throws IOException{
        if (in != null) {
            try {
                in.close();
            }finally {
                in = null;
            }
        }

    }

    private void advance() throws IOException{
        close();
        if (it.hasNext()) {
            in = it.next().openStream();
        }
    }

    public int available()throws IOException{
        if (in == null) {
            return 0;
        }
        return in.available();
    }

    @Override
    public boolean markSupported() {
        return false;
    }


    @Override
    public int read() throws IOException {
        while (in != null) {
            int result = in.read();
            if (result != -1) {
                return result;
            }
            advance();
        }
        return -1;
    }

    public int read(byte[] b, int off, int len) throws IOException{
        checkNotNull(b);
        while (in != null) {
            int result = in.read(b, off, len);
            if (result != -1) {
                return result;
            }
            advance();
        }
        return -1;
    }

    public long skip(long n) throws IOException {
        if (in == null || n <= 0) {
            return 0;
        }

        long result = in.skip(n);

        if (result != 0) {
            return result;
        }

        if (read() == -1) {
            return 0;
        }

        return 1 + in.skip(n-1);

    }



}
